package com.yj.ccs.abs.boot.config.swagger;

import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/13
 * Author     : Unbelievable9
 **/
@Configuration
@EnableSwagger2
@Profile({"dev", "debug"})
public class SwaggerConfig {

    private ApiInfo commonApiInfo() {
        return new ApiInfoBuilder()
                .title("瑞茂通ABS接口文档")
                .description("共通 RESTful API")
                .contact(new Contact("赵圈羊", "https://unbelievable9.info", "zhaoliyang@yunjianvc.com"))
                .version("0.0.1-dev")
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("瑞茂通ABS接口文档")
                .description("Web相关 RESTful API")
                .contact(new Contact("赵圈羊", "https://unbelievable9.info", "zhaoliyang@yunjianvc.com"))
                .version("0.0.1-dev")
                .build();
    }

    private ApiInfo oaApiInfo() {
        return new ApiInfoBuilder()
                .title("瑞茂通ABS接口文档")
                .description("OA相关 RESTful API")
                .contact(new Contact("赵圈羊", "https://unbelievable9.info", "zhaoliyang@yunjianvc.com"))
                .version("0.0.1-dev")
                .build();
    }


    private Predicate<String> commonOnlyEndpoints() {
        return new Predicate<String>() {
            @Override
            public boolean apply(String s) {
                return !s.contains("/web") && !s.equals("oa");
            }
        };
    }

    private Predicate<String> webOnlyEndpoints() {
        return new Predicate<String>() {
            @Override
            public boolean apply(String s) {
                return s.contains("/web");
            }
        };
    }

    private Predicate<String> oaOnlyEndpoints() {
        return new Predicate<String>() {
            @Override
            public boolean apply(String s) {
                return s.contains("/oa");
            }
        };
    }

    @Bean
    @Profile("dev")
    public Docket devCommonDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Common-RESTful-API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yj.ccs.abs.common"))
                .paths(commonOnlyEndpoints())
                .build()
                .apiInfo(commonApiInfo())
                .pathMapping("/");
    }

    @Bean
    @Profile("dev")
    public Docket devWebDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Web-RESTful-API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yj.ccs.abs.web"))
                .paths(webOnlyEndpoints())
                .build()
                .apiInfo(webApiInfo())
                .pathMapping("/");
    }

    @Bean
    @Profile("dev")
    public Docket devOaDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("OA-RESTful-API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yj.ccs.abs.oa"))
                .paths(oaOnlyEndpoints())
                .build()
                .apiInfo(oaApiInfo())
                .pathMapping("/");
    }

    @Bean
    @Profile("debug")
    public Docket debugCommonDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Common-RESTful-API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yj.ccs.abs.common"))
                .paths(commonOnlyEndpoints())
                .build()
                .apiInfo(commonApiInfo())
                .pathMapping("/abs");
    }

    @Bean
    @Profile("debug")
    public Docket debugWebDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("Web-RESTful-API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yj.ccs.abs.web"))
                .paths(webOnlyEndpoints())
                .build()
                .apiInfo(webApiInfo())
                .pathMapping("/abs");
    }

    @Bean
    @Profile("debug")
    public Docket debugOaDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("OA-RESTful-API")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yj.ccs.abs.oa"))
                .paths(oaOnlyEndpoints())
                .build()
                .apiInfo(oaApiInfo())
                .pathMapping("/abs");
    }
}
